package com.example.pinkcat.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.pinkcat.R;
import com.example.pinkcat.activity.VideoPinkcat;
import com.example.pinkcat.activity.ViewAllMyFollow;
import com.example.pinkcat.activity.api.entity.VideoEntity;
import com.example.pinkcat.api.entity.VideowaibuEntity;
import com.example.pinkcat.api.getData.getData_waibu;
import com.example.pinkcat.fragment.shouye.Fragment_zhuifan;
import com.rishabhharit.roundedimageview.RoundedImageView;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;
import com.youth.banner.listener.OnBannerListener;
import com.youth.banner.loader.ImageLoader;
import com.example.pinkcat.activity.pinkCatUser.userStaticMsg;
import com.example.pinkcat.api.entity.myCollectStaticMsg;


import java.util.ArrayList;
import java.util.List;

public class Zhuifan_m2_RecyclerViewAdapter extends RecyclerView.Adapter<Zhuifan_m2_RecyclerViewAdapter.ViewHolder> {
    public Context context;
    private Fragment_zhuifan fragment_zhuifan;
    Zhuifan_m1_RecyclerViewAdapter zhuifanm1;

    int count = 0;

    List<VideowaibuEntity> videobannerEnt=new ArrayList<>();
    List<VideowaibuEntity> videoMsgArrayList=new ArrayList<>();




    public Zhuifan_m2_RecyclerViewAdapter(Context context) {
        this.context = context;
    }

    public void addBannerData(List<VideowaibuEntity> videoEntities){
        this.videobannerEnt = videoEntities;
        notifyDataSetChanged();
    }

    public void addData(List<VideowaibuEntity> videoEntities){
        this.videoMsgArrayList=videoEntities;
        notifyDataSetChanged();
    }


    public static class ViewHolder extends RecyclerView.ViewHolder{
        public RoundedImageView imageView_nomorl;
        public TextView textViewDescription_nomorl;
        public TextView textViewTitlte_nomorl;

        public RecyclerView recyclerView;

        public Banner mBanner;

        private View video;

        public RelativeLayout zhuifan_rl;

        public TextView no_zhuifan;

        public TextView zf_ys_list;

        public RelativeLayout btn_ck;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            imageView_nomorl=itemView.findViewById(R.id.zhuifan_list_normsl_img);
            textViewTitlte_nomorl=itemView.findViewById(R.id.zhuifan_list_normsl_tv1);
            textViewDescription_nomorl=itemView.findViewById(R.id.zhuifan_list_normsl_tv2);

            recyclerView = itemView.findViewById(R.id.zhuifan_RV_show_1);
            no_zhuifan = itemView.findViewById(R.id.zhuifan_RV_show_1_nocollect);

            mBanner = itemView.findViewById(R.id.zf_ys_mBanner);

            video = itemView.findViewById(R.id.zhuivideo_normal);

            zhuifan_rl = itemView.findViewById(R.id.zhuifan_rl_1);

            zf_ys_list = itemView.findViewById(R.id.zf_ys_list_tv1);

            btn_ck = itemView.findViewById(R.id.btn_ck);
        }
    }


    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view;
        if (viewType == 0){
            view= LayoutInflater.from(context).inflate(R.layout.layout_banner_zf_ys,parent,false);
        }  else if (viewType == 1 && !userStaticMsg.userId.equals("-1")){
            view= LayoutInflater.from(context).inflate(R.layout.list_zhuifan,parent,false);//recyclerview的pager
        }else {
            view = LayoutInflater.from(context).inflate(R.layout.layout_zhuifan_list_nomrol,parent,false);//返回普通图片
        }

        return new Zhuifan_m2_RecyclerViewAdapter.ViewHolder(view);
    }

    @Override
    public int getItemViewType(int position) {
        if(position == 0){
            return 0;
        }
        else if ( position == 1 &&!userStaticMsg.userId.equals("-1")){
            return 1;
        }else {
            return 2;
        }
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

        //页面跳转

        if (position == 0) {
            initViews(holder,position);
            if (!userStaticMsg.userId.equals("-1")){
                holder.zf_ys_list.setVisibility(View.GONE);
            }
        }
        else if(position == 1&&!userStaticMsg.userId.equals("-1")) {
            if (videoMsgArrayList.size()==0)return;
            holder.recyclerView.setLayoutManager(new LinearLayoutManager(context,LinearLayoutManager.HORIZONTAL,false));
            zhuifanm1 = new Zhuifan_m1_RecyclerViewAdapter(context);
            getData_waibu.getMyVideo(context,zhuifanm1,0,userStaticMsg.userId);
            holder.recyclerView.setAdapter(zhuifanm1);

                if (myCollectStaticMsg.mycollect_zf!=0){
                    holder.zhuifan_rl.setVisibility(View.VISIBLE);
                    holder.recyclerView.setVisibility(View.VISIBLE);
                    holder.no_zhuifan.setVisibility(View.GONE);
                }else {
                    holder.zhuifan_rl.setVisibility(View.VISIBLE);
                    holder.recyclerView.setVisibility(View.GONE);
                    holder.no_zhuifan.setVisibility(View.VISIBLE);
                }

            //查看全部按钮
            holder.btn_ck.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (myCollectStaticMsg.mycollect_zf==0){
                        Toast.makeText(context,"你还没有收藏的视频，快去找找收藏到我的追番里吧",Toast.LENGTH_SHORT).show();
                    }else {
                        Intent intent = new Intent(context, ViewAllMyFollow.class);
                        intent.putExtra("position",1);
                        context.startActivity(intent);}
                }
            });

        }
        else if(!userStaticMsg.userId.equals("-1")){

                holder.textViewTitlte_nomorl.setText(videoMsgArrayList.get(position-2).getVideo_name());//视频标题
                holder.textViewDescription_nomorl.setText(videoMsgArrayList.get(position-2).getVideo_info());//视频描述
                Glide.with(context).load(Uri.parse(videoMsgArrayList.get(position-2).getVideo_cover())).into(holder.imageView_nomorl);//视频图片

                holder.video.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent=new Intent(context, VideoPinkcat.class);
                    intent.putExtra("v_id",videoMsgArrayList.get(position-2).getVideo_id());
                    intent.putExtra("up_id",videoMsgArrayList.get(position-2).getUser_id());
                    intent.putExtra("v_link",videoMsgArrayList.get(position-2).getVideo_link());
                    intent.putExtra("v_labs",videoMsgArrayList.get(position-2).getVideo_lab());
                    context.startActivity(intent);
                }
            });



        }else {
            holder.textViewTitlte_nomorl.setText(videoMsgArrayList.get(position-1).getVideo_name());//视频标题
            holder.textViewDescription_nomorl.setText(videoMsgArrayList.get(position-1).getVideo_info());//视频描述
            Glide.with(context).load(Uri.parse(videoMsgArrayList.get(position-1).getVideo_cover())).into(holder.imageView_nomorl);//视频图片

            holder.video.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent=new Intent(context, VideoPinkcat.class);
                    intent.putExtra("v_id",videoMsgArrayList.get(position-1).getVideo_id());
                    intent.putExtra("up_id",videoMsgArrayList.get(position-1).getUser_id());
                    intent.putExtra("v_link",videoMsgArrayList.get(position-1).getVideo_link());
                    intent.putExtra("v_labs",videoMsgArrayList.get(position-1).getVideo_lab());
                    context.startActivity(intent);
                }
            });
        }
        }



    @Override
    public int getItemCount() {

        if (!userStaticMsg.userId.equals("-1")){
            return videobannerEnt.size()==0? videoMsgArrayList.size():videoMsgArrayList.size()+2;
        }else{
            return videobannerEnt.size()==0? videoMsgArrayList.size():videoMsgArrayList.size()+1;
        }



    }


    private void initViews(Zhuifan_m2_RecyclerViewAdapter.ViewHolder viewHolder,int position) {


        //图片资源
        List<Uri> imageyriList = new ArrayList<>();
        //轮播标题
        List<String> titileList = new ArrayList<>();

        if (videobannerEnt.size() <=0 ) return;
        if (videobannerEnt.size()<4){
        for(int i=0; i<videobannerEnt.size(); i++){
            imageyriList.add(Uri.parse(videobannerEnt.get(position+i).getVideo_cover())); //把图片资源循环加入list里面
            titileList.add(String.valueOf(videobannerEnt.get(position+i).getVideo_name()));//把标题循环设置进列表里
            viewHolder.mBanner.setImageLoader(new ImageLoader() {
                @Override
                public void displayImage(Context context, Object path, ImageView imageView) {
                    Glide.with(context).load(path).fitCenter().into(imageView);
                }
            });}
        }else {
            for(int i=0; i<3; i++){
                imageyriList.add(Uri.parse(videobannerEnt.get(position+i).getVideo_cover())); //把图片资源循环加入list里面
                titileList.add(String.valueOf(videobannerEnt.get(position+i).getVideo_name()));//把标题循环设置进列表里

                viewHolder.mBanner.setImageLoader(new ImageLoader() {
                    @Override
                    public void displayImage(Context context, Object path, ImageView imageView) {
                        Glide.with(context).load(path).fitCenter().into(imageView);
                    }
                });}
        }

            //设置轮播的动画效果，可以去github上查看文档
            viewHolder.mBanner.setBannerAnimation(Transformer.Accordion);
            //设置图片资源
            viewHolder.mBanner.setImages(imageyriList);
            //设置自动轮播，默认为true
            viewHolder.mBanner.isAutoPlay(true);
            //设置样式,默认为:Banner.NOT_INDICATOR(不显示指示器和标题)
            //可选样式如下:
            //1. Banner.CIRCLE_INDICATOR    显示圆形指示器
            //2. Banner.NUM_INDICATOR   显示数字指示器
            //3. Banner.NUM_INDICATOR_TITLE 显示数字指示器和标题
            //4. Banner.CIRCLE_INDICATOR_TITLE  显示圆形指示器和标题
            //设置图片加载器,通过Glide加载图片
            //设置banner显示的样式（带标题的样式）
            viewHolder.mBanner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR_TITLE_INSIDE);

            //设置标题集合（当banner样式有显示title时）
            viewHolder.mBanner.setBannerTitles(titileList);
            //设置是否允许手动滑动轮播图
            viewHolder.mBanner.setViewPagerIsScroll(true);
            //设置指示器位置(即图片下面的那个小圆点)
            //设置轮播样式（没有标题默认为右边,有标题时默认左边）
            //可选样式:
            //Banner.LEFT   指示器居左
            //Banner.CENTER 指示器居中
            //Banner.RIGHT  指示器居右
            viewHolder.mBanner.setIndicatorGravity(BannerConfig.CENTER);
            //设置轮播时间3秒切换下一图
            viewHolder.mBanner.setDelayTime(3000);
            viewHolder.mBanner.setImages(imageyriList).setOnBannerListener(new OnBannerListener() {
                        @Override
                        public void OnBannerClick(int position) {
                            if (position == 0){
                                Intent intent = new Intent(context,VideoPinkcat.class);
                                intent.putExtra("v_id",videobannerEnt.get(position).getVideo_id());
                                intent.putExtra("up_id",videobannerEnt.get(position).getUser_id());
                                intent.putExtra("v_link",videobannerEnt.get(position).getVideo_link());
                                intent.putExtra("v_labs",videobannerEnt.get(position).getVideo_lab());
                                context.startActivity(intent);
                            }else if (position ==1){
                                Intent intent = new Intent(context,VideoPinkcat.class);
                                intent.putExtra("v_id",videobannerEnt.get(position).getVideo_id());
                                intent.putExtra("up_id",videobannerEnt.get(position).getUser_id());
                                intent.putExtra("v_link",videobannerEnt.get(position).getVideo_link());
                                intent.putExtra("v_labs",videobannerEnt.get(position).getVideo_lab());
                                context.startActivity(intent);
                            }else {
                                Intent intent = new Intent(context,VideoPinkcat.class);
                                intent.putExtra("v_id",videobannerEnt.get(position).getVideo_id());
                                intent.putExtra("up_id",videobannerEnt.get(position).getUser_id());
                                intent.putExtra("v_link",videobannerEnt.get(position).getVideo_link());
                                intent.putExtra("v_labs",videobannerEnt.get(position).getVideo_lab());
                                context.startActivity(intent);
                            }
                        }
                    }).start();
            viewHolder.mBanner.startAutoPlay();
        }
    }

